<?php
/**
 * Created by PhpStorm.
 * User: xiaomeng
 * Date: 17-8-31
 * Time: 下午2:29
 */
namespace swf\core;

class Filter
{
    /**
     * 过滤变量使输入的代码安全
     * @param $variable
     * @return string
     */
    static public function escape($variable,$strip_tags = true){
        if(is_array($variable)){
            foreach($variable as $key => $value){
                $variable[$key] = self::escape($value,$strip_tags);
            }
        }else{
            //HTML转义
            $variable = $strip_tags === true?strip_tags($variable):$variable;
            $variable = htmlspecialchars($variable, ENT_QUOTES, 'utf-8');
            //启用了magic_quotes
            if (!get_magic_quotes_gpc())
            {
                $variable = addslashes($variable);
            }
        }
        return $variable;
    }
}